package com.tjsoft.system.service;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.tjsoft.system.dao.CompDAO;
import com.tjsoft.system.dao.GovmoneyDAO;
import com.tjsoft.system.dao.MoneyitemDAO;
import com.tjsoft.system.dao.ReportDAO;
import com.tjsoft.system.dao.SelfmoneyDAO;
import com.tjsoft.system.dao.UserDAO;
import com.tjsoft.system.entity.base.Comp;
import com.tjsoft.system.entity.base.Govmoney;
import com.tjsoft.system.entity.base.Moneyitem;
import com.tjsoft.system.entity.base.Report;
import com.tjsoft.system.entity.base.Selfmoney;
import com.tjsoft.system.entity.base.User;

@Service
@Transactional
public class AccountManager {
	
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private CompDAO compDAO;
	@Autowired
	private GovmoneyDAO govDAO;
	@Autowired
	private SelfmoneyDAO selfDAO;
	@Autowired
	private MoneyitemDAO itemDAO;
	@Autowired
	private ReportDAO reportDAO;
	
	
	public Page<Report> findPageReport(final Page<Report> page, final List<PropertyFilter> filters)throws Exception{
		try {
			Query q = userDAO.getSession().getNamedQuery("getReport");
			q.list();
			return reportDAO.findPage(page, filters);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void saveUser(User user) throws Exception{
		try {
			userDAO.save(user);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void deleteUser(Long id) throws Exception{
		try {
			userDAO.delete(id);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void saveComp(Comp comp) throws Exception{
		try {
			compDAO.save(comp);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void deleteComp(Long id) throws Exception{
		try {
			compDAO.delete(id);
		} catch (Exception e) {
			throw e;
		}
	}
	
	
	public void saveGovmoney(Govmoney govmoney) throws Exception{
		try {
			govDAO.save(govmoney);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void deleteGovmoney(Long id) throws Exception{
		try {
			govDAO.delete(id);
		} catch (Exception e) {
			throw e;
		}
	}
	@Transactional
	public void saveSelfmoney(Selfmoney selfmoney) throws Exception{
		try {
			if(selfmoney.getId()!=null){
				String hql = "delete from Moneyitem mi where mi.selfmoney=?";
				itemDAO.batchExecute(hql, selfmoney);
			}
			for(Moneyitem item : selfmoney.getItems()){
				if(item!=null){
					item.setSelfmoney(selfmoney);
					itemDAO.save(item);
				}
			}
			selfDAO.save(selfmoney);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void deleteSelfmoney(Long id) throws Exception{
		try {
			selfDAO.delete(id);
		} catch (Exception e) {
			throw e;
		}
	}
	

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public CompDAO getCompDAO() {
		return compDAO;
	}

	public void setCompDAO(CompDAO compDAO) {
		this.compDAO = compDAO;
	}

	public GovmoneyDAO getGovDAO() {
		return govDAO;
	}

	public void setGovDAO(GovmoneyDAO govDAO) {
		this.govDAO = govDAO;
	}

	public SelfmoneyDAO getSelfDAO() {
		return selfDAO;
	}

	public void setSelfDAO(SelfmoneyDAO selfDAO) {
		this.selfDAO = selfDAO;
	}

	public MoneyitemDAO getItemDAO() {
		return itemDAO;
	}

	public void setItemDAO(MoneyitemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}

	public ReportDAO getReportDAO() {
		return reportDAO;
	}

	public void setReportDAO(ReportDAO reportDAO) {
		this.reportDAO = reportDAO;
	}


	
	
}
